package com.tl.commonlibrary.ui.exception;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tl.commonlibrary.AppConfig;
import com.tl.commonlibrary.R;
import com.tl.commonlibrary.storage.configuration.PrefUtil;
import com.tl.commonlibrary.storage.filecache.ACache;
import com.tl.commonlibrary.ui.BaseFragmentActivity;
import com.tl.commonlibrary.ui.widget.TitleBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2017/7/27 0027.
 */

public class CrashActivity extends BaseFragmentActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, CrashActivity.class);
        context.startActivity(starter);
    }

    ListView listView;
    private CrashAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_crash);
        getTitleBar().addAction(new TitleBar.TextAction(getString(R.string.crash_clear)) {
            @Override
            public void performAction(View view) {
                new AlertDialog.Builder(CrashActivity.this).setMessage(R.string.crash_clear)
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ACache.get(CrashActivity.this, CrashHandler.KEY_CRASH).clear();
                        if (adapter != null) {
                            adapter.clear();
                        }
                    }
                }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();
            }
        });
        getTitleBar().setTitle(R.string.crash_title);
        listView = (ListView) findViewById(R.id.listView);
        ArrayList<String> datas = ACache.get(AppConfig.appContext,CrashHandler.KEY_CRASH).getAllAsString();
        if(datas != null && !datas.isEmpty()){
            Collections.reverse(datas);
            adapter = new CrashAdapter(this, datas);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    CrashDetailActivity.start(context, adapter.getItem(position));
                }
            });
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {

    }
}
